<?php

namespace Apps\AppApi\Method\User;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\UserFollow;
use Apps\Common\Models\BpFavorites;
use Apps\Common\Models\BeautyParlorTag;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Libs\BeautyParlor;

/**
 * 我的关注列表
 */
class MyFollow extends BaseMethod
{
    public function run()
    {
    	/*if(false == $this->verifyUserAuth()){
    		return false;
    	}*/
    	$user_id = intval($this->getDataItem('user_id', 0));
    	$type = intval($this->getDataItem('type', 1)); // 关注类型（1=》专家，2=》美人儿（普通用户），3=》美容院）
		$page = intval($this->getDataItem('page', 1));

		if ($user_id == 0) {
			return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, 'URL参数不全');
		}
		if (!in_array($type, ['1', '2', '3'])) {
			return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR, 'type类型错误！');
		}

		$limit = $this->getConfig()->limit;
		$offset = ($page - 1) * $limit;

		$UserFollow = new UserFollow();
		if ($type == 3) { // 美容院（待加入）
			$this->setResult($this->getFavorites($user_id, $offset, $limit));
		} elseif ($type == 2) { // 美人儿（普通用户）
			$this->setResult($UserFollow->getFollowList($user_id, $offset, $limit));
		} else { // 专家
			$this->setResult($UserFollow->getFollowExpertList($user_id, $offset, $limit));
		}
    }

	/**
	 * 获取用户关注的美容院列表
	 * @param $user_id
	 * @param int $offset
	 * @param int $limit
	 * @return mixed
	 */
	private function getFavorites($user_id, $offset = 0, $limit = 10)
	{

		$list = BpFavorites::query()->columns([
			'p.bp_id id',
			'p.bp_name name',
			'p.bp_cover cover',
			'p.bp_address address',
			'p.bp_tel tel',
			'p.bp_medal medal_verify',
			'a.low_price',
			'a.facilities'
		])->leftJoin('Apps\Common\Models\BeautyParlor', 'Apps\Common\Models\BpFavorites.bp_id = p.bp_id', 'p')->leftJoin('Apps\Common\Models\BeautyParlorAttr', 'Apps\Common\Models\BpFavorites.bp_id = a.beauty_parlor_id', 'a')->where('p.bp_state = 1 and user_id = ' . $user_id)->limit($limit, $offset)->execute()->toArray();

		$BeautyParlorTag = new BeautyParlorTag();
		foreach ($list as $k => $val) {
			$tags = [];
			if ($tagList = $BeautyParlorTag->getTagList($val['id'])) {
				foreach ($tagList as $key => $value) {
					$tags[] = $value['tag_title'];
				}
			} else {
				$tags = [];
			}
			$list[$k]['cover'] = PicUrl::BeautyParlorCover($val['cover'], $this->getDi());
			$list[$k]['medal_verify'] = BeautyParlor::getMedalVerify($val['medal']);
			$list[$k]['facilities'] = $val['facilities'] ? json_decode($val['facilities'], true) : [];
			$list[$k]['tags'] = $tags;
		}
		return $list;
	}


}